package com.ruangong.GitClient.app.Password;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.ruangong.GitClient.R;

import java.util.ArrayList;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class PasswordActivity extends AppCompatActivity {
    private ListView websiteListView;
    private FloatingActionButton addButton;
    private ArrayList<Password> websiteList;
    private PasswordAdapter websiteAdapter;
    private PasswordDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        websiteListView = findViewById(R.id.websiteListView);
        addButton = findViewById(R.id.addButton);
        dbHelper = new PasswordDBHelper(this);

        // 初始化密码列表数据
        websiteList = getPasswordsFromDatabase();

        // 初始化适配器
        websiteAdapter = new PasswordAdapter(this, websiteList);

        // 将适配器与 ListView 关联
        websiteListView.setAdapter(websiteAdapter);

        // ListView点击事件，处理点击密码条目的逻辑
        websiteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取被点击的密码条目
                Password clickedPassword = websiteList.get(position);

                // 创建一个意图，启动密码详情页面，并将密码详情传递过去
                Intent intent = new Intent(PasswordActivity.this, PasswordDetailActivity.class);
                intent.putExtra("PASSWORD_DETAIL", clickedPassword);
                startActivity(intent);
            }
        });

        // 添加密码按钮点击事件，跳转到添加密码界面
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PasswordActivity.this, AddPasswordActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在活动恢复时更新密码列表
        updatePasswordList();
    }

    private ArrayList<Password> getPasswordsFromDatabase() {
        ArrayList<Password> passwordList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        // 查询数据库获取密码列表数据
        Cursor cursor = db.query(
                PasswordDBHelper.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );

        // 遍历游标并将数据添加到列表中
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(PasswordDBHelper.COLUMN_ID));
            @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex(PasswordDBHelper.COLUMN_USERNAME));
            @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex(PasswordDBHelper.COLUMN_PASSWORD));
            @SuppressLint("Range") String website = cursor.getString(cursor.getColumnIndex(PasswordDBHelper.COLUMN_WEBSITE));

            Password item = new Password(id, username, password, website);
            passwordList.add(item);
        }

        cursor.close();
        return passwordList;
    }

    private void updatePasswordList() {
        websiteList.clear();
        websiteList.addAll(getPasswordsFromDatabase());
        websiteAdapter.notifyDataSetChanged();
    }
}


